package contract.jvm;

import java.util.HashMap;
import java.util.Map;

/**
 * 堆
 */
public class Heap {

    /**
     * 对象实例保存在这个Map中
     */
    private final Map<String, Map<String, Object>> objects = new HashMap<>();

    /**
     * 获取对象字段的值
     */
    public Object getField(Reference reference, String fieldName) {
        return getFields(reference.toString()).get(fieldName);
    }

    /**
     * 对象字段赋值
     */
    public void putField(Reference reference, String fieldName, Object value) {
        getFields(reference.toString()).put(fieldName, value);
    }

    private Map<String, Object> getFields(String key) {
        if (!objects.containsKey(key)) {
            objects.put(key, new HashMap<>());
        }
        return objects.get(key);
    }

}
